package cn.controller;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import cn.pojo.Books;
import cn.service.BooksService;
import cn.service.BooksServiceImpl;

@WebServlet("*.html")
public class MainController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private BooksService booksService = new BooksServiceImpl();
	private Logger log = Logger.getLogger(MainController.class);

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.设置字符集
		request.setCharacterEncoding("utf-8");
		// 2. 获得Servlet请求路径
		String path = request.getServletPath();// path.html
		path = path.substring(1, path.indexOf("."));
		try {
			// 3.执行当前对象path方法
			Class<?> cl = Class.forName("cn.controller.MainController");
			Method mt = cl.getDeclaredMethod(path, HttpServletRequest.class, HttpServletResponse.class);
			mt.invoke(this, request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// 4、有任何异常返回登录界面，显示错误信息
			request.getSession().setAttribute("ERROR", "此方法不存在");
			// response.sendRedirect("jsp/books.jsp");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	protected void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String select = request.getParameter("select");
		String input = request.getParameter("input");
		//System.out.println(select);
		//System.out.println(input);
		List<Books> list = new ArrayList<Books>();
		// list = booksService.getBooksByName(input);
		if (select.equals("name")) {
			list = booksService.getBooksByName(input);
		}
		if (select.equals("author")) {
			list = booksService.getBooksByAuthor(input);
		}
		if (select.equals("publish")) {
			list = booksService.getBooksByPublish(input);
		}
//		for (Books b : list) {
//			System.out.println(b);
//		}
		HttpSession session = request.getSession();
		session.setAttribute("LIST", list);
		request.getRequestDispatcher("jsp/books.jsp").forward(request, response);
	}
	protected void addBooks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String name=request.getParameter("name");
		String author=request.getParameter("author");
		String publish=request.getParameter("publish");
		String msg1=request.getParameter("page");
		String msg2=request.getParameter("price");
		Books books=new Books();
		books.setName(name);
		books.setAuthor(author);
		books.setPublish(publish);
		int page=0;
		int price=0;
		try {
			page=Integer.parseInt(msg1);
			price=Integer.parseInt(msg2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		books.setPage(page);
		books.setPrice(price);
		//System.out.println(books);
		int i=booksService.addBooks(books);
		HttpSession session=request.getSession();
		session.setAttribute("I", i);
		request.getRequestDispatcher("jsp/books.jsp").forward(request, response);
	}
}
